package com.common.repostitory.impl;

import com.common.repostitory.hibernate.RowMapping;
import com.common.service.CommonService;
import com.common.tool.Page;
import com.ssh.entity.Consumer;
import com.common.repostitory.ConsumerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by zpen on 2018/2/24.
 */
@Repository("consumerRepository")
@Transactional
public class ConsumerRepositoryImpl extends BaseRepostitoryImpl<Consumer,Long> implements ConsumerRepository  {



    public Consumer getConsumer(Consumer consumer) {
      return load(Consumer.class,consumer.getId());
    }

    public int getAllRowCount(Consumer consumer) {
        StringBuffer querySql = new StringBuffer();
        List paramList = new ArrayList();
        querySql.append("select count(*) from consumer where 1=1 and status <>'90'  ");
        if(!StringUtils.isBlank(consumer.getUsername())){
            querySql.append("and username = ? ");
            paramList.add(consumer.getUsername());
        } if(!StringUtils.isBlank(consumer.getAddress())){
            querySql.append("and address = ? ");
            paramList.add(consumer.getAddress());
        } if(!StringUtils.isBlank(consumer.getPhone())){
            querySql.append("and phone = ? ");
            paramList.add(consumer.getPhone());
        } if(!StringUtils.isBlank(consumer.getRemark())){
            querySql.append("and remark = ?");
            paramList.add(consumer.getRemark());
        }if(!StringUtils.isBlank(consumer.getStatus())){
            querySql.append("and status =?");
            paramList.add(consumer.getStatus());
        }
        int count = jdbcTemplate.queryForObject(querySql.toString(),paramList.toArray(),Integer.class);
        return count;
    }

    public List<Map<String, Object>> queryForPage(Consumer consumer, Page page) {
        StringBuffer querySql = new StringBuffer();
        List paramList = new ArrayList();
        querySql.append("select * from consumer where 1=1 and status <>'90' ");
        if(!StringUtils.isBlank(consumer.getUsername())){
            querySql.append("and username = ? ");
            paramList.add(consumer.getUsername());
        } if(!StringUtils.isBlank(consumer.getAddress())){
            querySql.append("and address = ? ");
            paramList.add(consumer.getAddress());
        } if(!StringUtils.isBlank(consumer.getPhone())){
            querySql.append("and phone = ? ");
            paramList.add(consumer.getPhone());
        } if(!StringUtils.isBlank(consumer.getRemark())){
            querySql.append("and remark = ? ");
            paramList.add(consumer.getRemark());
        }if(!StringUtils.isBlank(consumer.getStatus())){
            querySql.append("and status =? ");
            paramList.add(consumer.getStatus());
        }
        if(!StringUtils.isBlank(page.getSort())){
            querySql.append("order by ? ?");
            paramList.add(page.getSort());
            paramList.add(page.getOrder());
        }
        querySql.append(" limit ?,? ");
        paramList.add(Integer.valueOf(page.getOffset()));
        paramList.add(Integer.valueOf(Integer.valueOf(page.getLimit())));
        List<Map<String,Object>> list=  jdbcTemplate.queryForList(querySql.toString(),paramList.toArray());
        return list;
    }

}
